package com.ruoyi.channelApi;

import com.ruoyi.channelApi.domain.ChannelRequest;
import com.ruoyi.channelApi.domain.bo.TChannelPushBo;
import com.ruoyi.channelApi.domain.vo.TChannelPushVo;
import com.ruoyi.channelApi.service.ITChannelPushService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 渠道推送记录
 *
 * @author ruoyi
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/channel/channelPush")
public class TChannelPushController extends BaseController {

    private final ITChannelPushService iTChannelPushService;

    /**
     * 查询渠道推送记录列表
     */
    //@SaCheckPermission("channel:channelPush:list")
    @GetMapping("/list")
    public TableDataInfo<TChannelPushVo> list(TChannelPushBo bo, PageQuery pageQuery) {
        return iTChannelPushService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出渠道推送记录列表
     */
    //@SaCheckPermission("channel:channelPush:export")
    @Log(title = "渠道推送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TChannelPushBo bo, HttpServletResponse response) {
        List<TChannelPushVo> list = iTChannelPushService.queryList(bo);
        ExcelUtil.exportExcel(list, "渠道推送记录", TChannelPushVo.class, response);
    }

    /**
     * 获取渠道推送记录详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("channel:channelPush:query")
    @GetMapping("/{id}")
    public R<TChannelPushVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        TChannelPushVo channelPushVo = Optional.ofNullable(iTChannelPushService.queryById(id))
                .orElseThrow(() -> new ServiceException("推送信息没有找到"));
        channelPushVo.setChannelRequest(JsonUtils.parseObject(channelPushVo.getDataInfo(), ChannelRequest.class));
        return R.ok(channelPushVo);
    }

    /**
     * 新增渠道推送记录
     */
    //@SaCheckPermission("channel:channelPush:add")
    @Log(title = "渠道推送记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TChannelPushBo bo) {
        return toAjax(iTChannelPushService.insertByBo(bo));
    }

    /**
     * 修改渠道推送记录
     */
    //@SaCheckPermission("channel:channelPush:edit")
    @Log(title = "渠道推送记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TChannelPushBo bo) {
        return toAjax(iTChannelPushService.updateByBo(bo));
    }

    /**
     * 删除渠道推送记录
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("channel:channelPush:remove")
    @Log(title = "渠道推送记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTChannelPushService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
